package com.aipii.demo.activiti;

import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shirdrn
 */
@ContextConfiguration({
		"classpath:application-context.xml"})
public class ActivitiWithSpringTest2 extends AbstractSpringTest {
	private static Logger logger = LoggerFactory.getLogger(ActivitiWithSpringTest2.class);
	
	@Test
	public void testDeploy() throws Exception {
		Deployment deployment = repositoryService
				.createDeployment()
				.addClasspathResource("approve.bpmn")
				.name("测试流程")
				.deploy();
		deploymentId = deployment.getId();
		logger.debug("流程部署成功,流程ID为: " + deploymentId);
	}
	
	@Test
	public void testDelete() throws Exception {
		repositoryService.deleteDeployment("60001", true);
	}
	
	@Test
	public void startMyProcess() {
		//开启流程，myprocess是流程的ID
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("createUserProcess");
		String pid = processInstance.getId();
		logger.debug("processInstance.id = " + pid);
		//利用taskservice进行任务查询，查询第一个任务，查询后完成
		Task task = taskService.createTaskQuery()
				.processInstanceId(pid)
				.singleResult();
		Map<String, Object> vars = new HashMap<>();
		ParamForm form = new ParamForm();
		form.setPid(pid);
		form.setApplicant("guest");
		form.setApplicantTime(new Date());
		form.setApprover("admin");
		vars.put("form", form);
		vars.put("approver", null);
		logger.debug("申请任务--->" + task.getName() + " , taskId: " + task.getId());
		taskService.complete(task.getId(), vars);
	}
	
	@Test
	public void testApprove() {
		//完成第一个任务后再次查询，出现第二个任务名称，完成第二个任务
		Task task = taskService.createTaskQuery()
				.processInstanceId("65001")
				.singleResult();
		logger.debug("执行审批--->" + task.getName() + " , taskId: " + task.getId());
		Map<String, Object> vars = taskService.getVariables(task.getId());
		ParamForm form = (ParamForm) vars.get("form");
		form.setApprovalTime(new Date());
		form.setApprovalState("true");
		vars.put("flag", true);
		taskService.complete(task.getId(), vars);
	}
	
	@Test
	public void startActive() {
		Task task = taskService.createTaskQuery().taskCandidateGroup("active").singleResult();
		logger.debug("拒绝激活--->" + task.getName() + " , taskId: " + task.getId());
		Map<String, Object> vars = taskService.getVariables(task.getId());
		ParamForm form = (ParamForm) vars.get("form");
		form.setActivator("superAdmin");
		form.setActivationTime(new Date());
		form.setActivationState("false");
		vars.put("flag", false);
		taskService.complete(task.getId(), vars);
	}
	
	@Test
	public void endActive() {
		Task task = taskService.createTaskQuery().singleResult();
		logger.debug("申请失败--->" + task.getName() + " , taskId: " + task.getId());
		Map<String, Object> param = new HashMap<>();
		taskService.complete(task.getId(), param);
		task = taskService.createTaskQuery().singleResult();
		logger.debug("end" + task);
	}
	
}